<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.keyword" placeholder="请输入学号或姓名"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="handleCurrentChange(1)">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table ref="stuTable" :data="students" highlight-current-row v-loading="listLoading" @selection-change="selsChange" @select="selectRow" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
            <el-table-column prop="snumber" label="学号" width="120" sortable>
            </el-table-column>
			<el-table-column prop="sname" label="姓名" width="120" sortable>
			</el-table-column>
			<el-table-column prop="gender" label="性别" width="100"  sortable>
			    <template scope="scope">
                    <span v-if="scope.row.gender==0">男</span>
                    <span v-else="scope.row.gender==1">女</span>
                </template>
            </el-table-column>
			<el-table-column prop="name.name" label="专业" width="100" sortable>
			</el-table-column>
            <el-table-column prop="birthday" label="生日" width="180">
            </el-table-column>
            <el-table-column prop="phone" label="手机号" width="120" sortable>
            </el-table-column>
			<el-table-column prop="address" label="地址" min-width="180" sortable>
			</el-table-column>
			<el-table-column label="操作" width="150">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.row.id)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="5" :total="total" style="float:right;">
			</el-pagination>
		</el-col>


        <!--新增界面-->
        <el-dialog title="新增" :visible.sync="addFormVisible" :close-on-clickmodal="false">
            <el-form :model="addForm" label-width="80px" :rules="addFormRules"
                     ref="addForm">
                <el-form-item label="学号" prop="snumber">
                    <el-input v-model="addForm.snumber" auto-complete="off">

                        </el-input>
                </el-form-item>
                <el-form-item label="姓名" prop="sname">
                    <el-input v-model="addForm.sname" auto-complete="off">
                    </el-input>
                </el-form-item>
                <el-form-item label="性别">
                    <el-radio-group v-model="addForm.gender">
                        <el-radio class="radio" :label="0">男</el-radio>
                        <el-radio class="radio" :label="1">女</el-radio>
                    </el-radio-group>

                </el-form-item>
                <el-form-item label="专业名称" prop="name.name">
                    <el-select v-model="addForm.name.id" placeholder="请选择">
                        <el-option
                                v-for="item in majors"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id">
                            <span style="float: left">{{ item.name }}</span>
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="生日" prop="birthday">
                    <el-input v-model="addForm.birthday" auto-complete="off" placeholder="格式:2022-10-01">
                    </el-input>
                </el-form-item>
                <el-form-item label="电话号码" prop="phone">
                    <el-input v-model="addForm.phone" auto-complete="off">
                    </el-input>
                </el-form-item>
                <el-form-item label="地址" prop="address">
                    <el-input v-model="addForm.address" auto-complete="off">
                    </el-input>
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="addFormVisible = false">取消</el-button>
                    <el-button type="primary" @click.native="addSubmit"
                               :loading="addLoading">提交
                    </el-button>
            </div>
        </el-dialog>

    </section>
</template>

<script>
	import util from '../../common/js/util'
	//import NProgress from 'nprogress'
	import { getStudentListPage, removeStudent, batchRemoveStudent, editStudent, addStudent } from '../../api/api';

	export default {
		data() {
			return {
				filters: {
					keyword: ''
				},
				students: [],
				total: 0,
				currentPage: 1,
				listLoading: false,
				sels: [],//列表选中列
                /*专业名称集合*/
                majors:[],
				addFormVisible: false,//新增界面是否显示
				addLoading: false,
				addFormRules: {
                    snumber: [
                        { required: true, message: '请输入学号', trigger: 'blur' }
                    ],
					sname: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					]
				},
				//新增界面数据
				addForm: {
					snumber: '',
                    sname: '',
                    gender: 0,
                    name: {},
                    birthday: '',
                    phone: '',
                    address: ''
				}

			}
		},
		methods: {
            selectRow(selection, row){
                //selection 所有选中行对象的数组 row 当前选中行对象
                console.log('选中某行----',selection,row)
                this.sels=selection;
            },
			handleCurrentChange(cPage) {
			    console.log('当前页-----',cPage)
				this.currentPage = cPage;
				this.getStudents();
			},
			//获取学生列表
			getStudents() {
				let para = {
					currentPage: this.currentPage,
					keyword: this.filters.keyword
				};
				this.listLoading = true;
				//发起分页查询请求
				this.$http.post('/stu',para).then((res) => {
				    console.log(res)
                    //查询总记录数
					this.total = res.data.totals;
				    //更新表格数据
					this.students = res.data.rows;
					this.listLoading = false;
				});
			},
			//删除
			handleDel: function (id) {
                console.log('要删除的id是---',id);
                this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;

					this.$http.delete('/stu/'+id).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.handleCurrentChange(1);
					});
				}).catch(() => {

				});
			},
			//显示编辑界面
			handleEdit: function (row) {
                this.dia
                this.title="编辑";
                console.log('------handleEdit',row);
                //显示对话框
                this.addFormVisible = true;
				this.addForm = Object.assign({}, row);
			},
			//显示新增界面
			handleAdd: function () {
				this.addFormVisible = true;

			},
			//编辑
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true;
							//NProgress.start();
							let para = Object.assign({}, this.editForm);
							para.birth = (!para.birth || para.birth == '') ? '' : util.formatDate.format(new Date(para.birth), 'yyyy-MM-dd');
							editStudent(para).then((res) => {
								this.editLoading = false;
								//NProgress.done();
								this.$message({
									message: '提交成功',
									type: 'success'
								});
								this.$refs['editForm'].resetFields();
								this.editFormVisible = false;
								this.getStudents();
							});
						});
					}
				});
			},
			//新增
			addSubmit: function () {
			    /*表单校验*/
				this.$refs.addForm.validate((valid) => {
				    /*判断是否通过校验*/
					if (valid) {
					    /*询问是否提交*/
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							//this.addLoading = true;
                            //收集参数 将表单绑定的数据 复制到另一个对象中 作为备用参数
							let para = Object.assign({}, this.addForm);
                            //查看备用参数
                            console.log('提交的参数----',para)
							para.birthday = (!para.birthday || para.birthday == '') ? '' : util.formatDate.format(new Date(para.birthday), 'yyyy-MM-dd');
							this.$http.put('/stu',para).then((res) => {
								this.addLoading = false;
								//NProgress.done();
								this.$message({
									message: '提交成功',
									type: 'success'
								});
								this.$refs['addForm'].resetFields();
								this.addFormVisible = false;
								//重新查询
								this.handleCurrentChange(1);
							});
						});
					}
				});
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
                console.log('批量删除----',this.sels)
                var ids = this.sels.map(item => item.id);
                console.log('遍历集合，收集id--',ids);
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;

					this.$http.patch('/stu',ids).then((res) => {
					    this.listLoading = false;

						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.handleCurrentChange(1);
					});
				}).catch(() => {

				});
			},
            /*查询所有专业名称的方法*/
            getMajors(){
			    this.$http.get('/major').then(res=>{
			        console.log(res.data);
			        //将查到的专业名称集合赋值给majors
			        this.majors =res.data;
                })
            }
		},
		mounted() {
			this.getStudents();
			this.getMajors();
		}
	}

</script>

<style scoped>

</style>